package com.ironone.streaming.application;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ironone.streaming.R;

public class CustomAlertDialog {

	private Dialog dialog;
	private Button btn_ok;
	private Button btn_cancel;
	private TextView heading_text;
	private TextView body_text;
	
	public CustomAlertDialog(Activity act, CusAlrtBtnTypes btnType, CusAlrtIconTypes iconType) {

		// Custom dialog
		dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialogbox);

		// Set the custom dialog components - text, image and button
		ImageView icon_image = (ImageView) dialog.findViewById(R.id.icon_image);
		heading_text = (TextView) dialog.findViewById(R.id.heading_text);
		heading_text.setTextSize(FormObject.getInstance().getTextSize());
		body_text = (TextView) dialog.findViewById(R.id.body_text);
		body_text.setTextSize(FormObject.getInstance().getTextSize() - 5);
		btn_ok = (Button) dialog.findViewById(R.id.btn_ok);
		btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
		LinearLayout alertbox_layout = (LinearLayout) dialog.findViewById(R.id.alertbox_layout);

		int scrnWidth = FormObject.getInstance().getScreenWidth();
		int btnWidthSize = (int)(scrnWidth * 0.20);
		if(btnWidthSize > 250) btnWidthSize = 250;
		int btnHeightSize = (int)(btnWidthSize * 0.35);
		btn_cancel.setWidth(btnWidthSize);
		btn_cancel.setHeight(btnHeightSize);
		btn_ok.setWidth(btnWidthSize);
		btn_ok.setHeight(btnHeightSize);

		LinearLayout.LayoutParams pParam = (LinearLayout.LayoutParams) icon_image.getLayoutParams();
		int imgMaxSize = (int)(scrnWidth * 0.25); 
		if(imgMaxSize > 250) imgMaxSize = 250;
		pParam.height = imgMaxSize;
		pParam.width = imgMaxSize;
		icon_image.setLayoutParams(pParam);

		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) alertbox_layout.getLayoutParams();
		//int layoutHeight = (int)(scrnWidth * 0.5);
		//if(layoutHeight > 300) layoutHeight = 300;
		//params.height = layoutHeight;
		params.width = scrnWidth;

		switch (iconType) {
		case BARN:
			icon_image.setImageResource(R.drawable.err_dialog_barn);
			break;
		case BIRD:
			icon_image.setImageResource(R.drawable.err_dialog_bird);
			break;
		case CALL:
			icon_image.setImageResource(R.drawable.err_dialog_call);
			break;
		case DUCK:
			icon_image.setImageResource(R.drawable.err_dialog_duck);
			break;
		case TAPE:
			icon_image.setImageResource(R.drawable.err_dialog_tape);
			break;
		case WIZERD:
			icon_image.setImageResource(R.drawable.err_dialog_gandalf);
			break;
		case SUCCESS:
			icon_image.setImageResource(R.drawable.err_dialog_shoe);
			break;
		default:
			icon_image.setImageResource(R.drawable.err_dialog_duck);
			break;
		}
		
		switch (btnType) {
		case OK:
			btn_ok.setVisibility(View.VISIBLE);
			btn_ok.setText("Ok");			
			break;
		case ACCEPT_DECLINE:
			btn_ok.setVisibility(View.VISIBLE);
			btn_ok.setText(R.string.accept);
			btn_cancel.setVisibility(View.VISIBLE);
			btn_cancel.setText(R.string.decline);
			break;
		case YESNO:
			btn_ok.setVisibility(View.VISIBLE);
			btn_ok.setText(R.string.yes);
			btn_cancel.setVisibility(View.VISIBLE);
			btn_cancel.setText(R.string.no);
			break;			
		default:
			btn_ok.setVisibility(View.VISIBLE);
			btn_ok.setText(R.string.ok);
			break;
		}
		
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
	}

	public void setTitleText(String text){
		if(text.trim().equals("")) return;
		heading_text.setVisibility(View.VISIBLE);
		heading_text.setText(text.trim());
	}
	
	public void setMessageText(String text){
		if(text.trim().equals("")) return;
		body_text.setVisibility(View.VISIBLE);
		body_text.setText(text.trim());
	}

	public Button setOkButtonClickEvent(){
		return btn_ok;
	}
	
	public Button setCancelButtonClickEvent(){
		return btn_cancel;
	}
	
	public void show() {
		dialog.show();
	}	

	public void dismiss(){
		dialog.dismiss();
	}
}
